import {
  Component,
  Injector,
  ViewEncapsulation,
  ViewChild,
  ElementRef,
  OnInit
} from "@angular/core";
import { AppComponentBase } from "@shared/common/app-component-base";
import {
  ComboboxItemDto,
  MainTaskServiceProxy,
  ReceiveTaskInput
} from "@shared/service-proxies/service-proxies";
import { appModuleAnimation } from "@shared/animations/routerTransition";
import { Table } from "primeng/components/table/table";
import { Paginator } from "primeng/components/paginator/paginator";
import { LazyLoadEvent } from "primeng/components/common/lazyloadevent";
import * as moment from "moment";
import { finalize } from "rxjs/operators";
import { PutIntoProductionModalComponent } from "@app/plan/maintasks/put-into-production-modal.component";
import { MenuItem } from "primeng/api";
@Component({
  templateUrl: "./maintask.component.html",
  styleUrls: ["./maintask.component.less"],
  encapsulation: ViewEncapsulation.None,
  animations: [appModuleAnimation()]
})
export class MaintaskComponent extends AppComponentBase implements OnInit {
  // @ViewChild('boundingTeamGroupModal') boundingTeamGroupModal: BoundingTeamGroupModalComponent;
  @ViewChild("putIntoProductionModal")
  putIntoProductionModal: PutIntoProductionModalComponent;
  // @ViewChild('createOrEditOrderModal') createOrEditOrderModal: CreateOrEditOrderModalComponent;

  @ViewChild("dataTable") dataTable: Table;
  @ViewChild("paginator") paginator: Paginator;
  //@ViewChild('orderCategoryCombobox') orderCategoryCombobox: ElementRef;
  @ViewChild("statusCombobox") statusCombobox: ElementRef;
  @ViewChild("teamGroupCombobox") teamGroupCombobox: ElementRef;

  advancedFiltersAreShown = false;
  loading: boolean;
  orderCategorys: ComboboxItemDto[];
  statuses: ComboboxItemDto[];
  teamGroups: ComboboxItemDto[];
  isReceived = true;
  isCompleted = false;
  anyncing = false;
  type = 1;
  filterText = "";
  organizationUnitId: number = undefined;
  dueDateBegin: moment.Moment = undefined;
  dueDateEnd: moment.Moment = undefined;
  tabs: MenuItem[];
  priority: number = undefined;

  constructor(
    injector: Injector,
    private _mainTaskServiceProxy: MainTaskServiceProxy
  ) {
    super(injector);
  }

  ngOnInit() {
    this.getTabs();
    this._mainTaskServiceProxy.getComboxItemsForSearch().subscribe(result => {
      this.statuses = result.statuses;
      this.teamGroups = result.teamGroups;
    });
    this.loading = true;
  }
  getModels(event?: LazyLoadEvent) {
    //this.loading = true;
    this.primengTableHelper.showLoadingIndicator();
    console.log(1111);
    console.log(this.primengTableHelper.isLoading);

    this._mainTaskServiceProxy
      .getMainTasks(
        this.filterText,
        this.isReceived,
        undefined,
        this.isCompleted,
        53,
        this.primengTableHelper.getSkipCount(this.paginator, event),
        this.primengTableHelper.getMaxResultCount(this.paginator, event)
      )
      .subscribe(result => {
        this.primengTableHelper.totalRecordsCount = result.totalCount;
        this.primengTableHelper.records = result.items;
        this.primengTableHelper.hideLoadingIndicator();
        // setTimeout(() => {
        //   this.loading = false;
        // }, 1000);
      });
  }
  receiveTask(id: number) {
    this.message.confirm("确认接收该条任务吗？", isConfirmed => {
      if (isConfirmed) {
        let rt = new ReceiveTaskInput();
        rt.id = id;
        rt.recipientUserId = 2;
        this._mainTaskServiceProxy.receiveTask(rt).subscribe(() => {
          this.notify.success("接收成功");
          this.getModels();
        });
      }
    });
  }
  deleteModel(id: number) {
    this.message.confirm("确认删除该条型号计划吗？", isConfirmed => {
      if (isConfirmed) {
        this._mainTaskServiceProxy.deleteMainTask(id).subscribe(() => {
          this.notify.success(this.l("SuccessfullyDeleted"));
          this.getModels();
        });
      }
    });
  }
  getTabs() {
    this.tabs = [
      { label: "已接收", icon: "fa fa-fw fa-bar-chart" },
      { label: "未接收", icon: "fa fa-fw fa-calendar" }
    ];
  }
  tabTypeClick(tab: any) {
    this.isCompleted = false;
    this.isReceived = true;
    if (tab == "已接收") {
      this.isReceived = true;
      this.type = 1;
    } else if (tab == "未接收") {
      this.isReceived = false;
      this.type = 2;
    }
    this.getModels();
  }
  BatchReceiveTask() {
    this._mainTaskServiceProxy
      .getMainTasks(
        this.filterText,
        false,
        undefined,
        this.isCompleted,
        53,
        0,
        100000
      )
      .subscribe(result => {
        //console.log(result);
        this.primengTableHelper.totalRecordsCount = result.totalCount;
        this.primengTableHelper.records = result.items;
        this.primengTableHelper.hideLoadingIndicator();
        let tt = result.items;
        tt.forEach(t => {
          let rt = new ReceiveTaskInput();
          rt.id = t.id;
          this._mainTaskServiceProxy.receiveTask(rt).subscribe(() => {});
        });
        this.getModels();
      });
  }
  manualWork() {
    this.message.confirm('确认同步吗？', isConfirmed => {
      if (isConfirmed) {
        this.anyncing = true;
        this._mainTaskServiceProxy
          .manualWork()
          .pipe(
            finalize(() => {
              this.anyncing = false;
              //this.primengTableHelper.isLoading=false;
            })
          )
          .subscribe(() => {
            this.notify.success('同步成功');
              // this.getModels();
          });
      }
    });

  }
}
